function check_username(username, hint) {
    if (!username) {
        hint.text('请输入用户名');
        return false;
    }
    if (username.length < 3 || username.length > 30) {
        hint.text('用户名长度应为3~30个字符');
        return false;
    }

    var re = /^[a-z_A-Z0-9一-龥]+$/;
    if (!re.test(username)) {
        hint.text('用户名只能包含中文字符、英文字母、数字和下划线(_)');
        return false;
    }

    return true;
}

function check_email(email, hint) {
    if (!email) {
        hint.text('请输入电子邮件');
        return false;
    }

    var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!re.test(email)) {
        hint.text('请输入正确的邮件地址');
        return false;
    }

    return true;
}

function check_password(input) {
    var password = $(input).val();
    var hint = $(input).next();
    if (password.length < 6) {
        hint.text('密码至少6位');
        return false;
    } else {
        hint.text('');
        return true;
    }
}

function check_password_repeat(input, password) {
    var password_repeat = $(input).val();
    var hint = $(input).next();
    if (password != password_repeat) {
        hint.text('两次输入的密码不一致');
        return false;
    } else {
        hint.text('');
        return true;
    }
}

$(document).ready(function(){
    $('#id_username').blur(function() {
        var username = $(this).val();
        var hint = $(this).next();
        if (!check_username(username, hint))
            return;

        $.ajax({
            url: '/accounts/check_username/',
            data: {username: username},
            success: function(msg) {
                var error = eval(msg).error;
                if (error)
                    hint.text(error);
                else
                    hint.text('');
            }
        });
    });

    $('#id_email').blur(function() {
        var email = $(this).val();
        var hint = $(this).next();
        if (!check_email(email, hint))
            return;

        $.ajax({
            url: '/accounts/check_email/',
            data: {email: $(this).val()},
            success: function(msg) {
                var error = eval(msg).error;
                if (error)
                    hint.text(error);
                else
                    hint.text('');
            }
        });
    });

    $('#id_password').blur(function() {
        check_password($(this));
    });

    $('#id_password_repeat').blur(function() {
        var password = $('#id_password').val();
        check_password_repeat($(this), password);
    });

    $('#signup_form').submit(function() {
        var username = $('#id_username').val();
        var username_hint = $('#id_username').next();
        var email = $('#id_email').val();
        var email_hint = $('#id_email').next();
        var password = $('#id_password').val();

        username_ok = check_username(username, username_hint);
        email_ok = check_email(email, email_hint);
        password_ok = check_password($('#id_password'));
        password_repeat_ok = check_password_repeat($('#id_password_repeat'), password);
        
        if (!username_ok) {
            $('#id_username').focus();
            return false;
        }
        if (!email_ok) {
            $('#id_email').focus();
            return false;
        }
        if (!password_ok) {
            $('#id_password').focus();
            return false;
        }
        if (!password_repeat_ok) {
            $('#id_password_repeat').focus();
            return false;
        }
        return true;
    });

    $('#reset_password_form').submit(function() {
        var password = $('#id_password').val();
        password_ok = check_password($('#id_password'));
        password_repeat_ok = check_password_repeat($('#id_password_repeat'), password);
        
        if (!password_ok) {
            $('#id_password').focus();
            return false;
        }
        if (!password_repeat_ok) {
            $('#id_password_repeat').focus();
            return false;
        }
        return true;
    });

});
